package cc.linker.steplink.crm.merchant.controller.acs;

import cc.linker.steplink.crm.commons.model.PcUserInfo;
import cc.linker.steplink.crm.merchanth5.commons.UserInfoUtil;
import cc.linker.steplink.crm.merchant.dao.acs.XbMerchantMapper;
import cc.linker.steplink.crm.merchant.service.acs.LinkAccountService;
import cc.linker.steplink.crm.merchanth5.dao.acs.PurchaseProcessDao;
import cc.linker.steplink.crm.merchanth5.service.MaiBaoLaService;
import cc.linker.steplink.crm.merchanth5.service.MallService;
import cc.linker.steplink.crm.merchanth5.service.acs.MerchantWalletService;
import cc.linker.steplink.crm.response.GlobalException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
资金账户管理
huhj
 */
@RequestMapping("/tl/linkaccount")
@RestController
public class LinkAccountController {


    @Autowired
    private LinkAccountService linkAccountService;
    @Autowired
    private MerchantWalletService merchantWalletService;
    @Autowired
    private MaiBaoLaService maiBaoLaService;
    @Autowired
    private PurchaseProcessDao purchaseProcessDao;
    @Autowired
    private XbMerchantMapper xbMerchantMapper;
    @Autowired
    private MallService mallService;
    @Autowired
    private UserInfoUtil userInfoUtil;

    /**
     * 资金账户获取列表
     * @return
     * @throws GlobalException
     */
    @GetMapping("/getaccountlist")
    public List getList() throws IOException, URISyntaxException {
        return linkAccountService.getList();
    }

    /**
     * 通过平台Id取联汇资金子账户列表
     * @param platformId
     * @return
     */
    @GetMapping("/getlinklist")
    public List getLinkList(@RequestParam Integer platformId){
        if(StringUtils.isEmpty(platformId)){
            throw GlobalException.build("参数错误");
        }
        return linkAccountService.getLinkList(platformId);
    }

    @GetMapping("/getaccountinfo")
    public Map getInfo(@RequestParam Integer accountRecordid){
        if(StringUtils.isEmpty(accountRecordid)){
            throw GlobalException.build("参数错误");
        }
        return linkAccountService.getInfo(accountRecordid);
    }

//    public PageInfo<Map<String, Object>> getList(@RequestParam(defaultValue = "20") Integer pageSize,
//                                                 @RequestParam(defaultValue = "1") Integer pageIndex
//    ) throws GlobalException {
//
//        PageHelper.startPage(pageIndex, pageSize);
//        return new PageInfo<>(linkAccountService.getList());
//    }


    /**
     * 获取通联支付所有机构列表
     * @return
     */
    @GetMapping("/getorglist")
    public List getOrgList() throws IOException, URISyntaxException {
        return linkAccountService.getOrgList();
    }

    /**
     * 单独发送手机验证码
     * @return
     */
    @PostMapping("/sendverificationcode")
    public boolean sendVerificationCode(@RequestBody Map<String, String> param, HttpServletRequest request) throws IOException, URISyntaxException {
        if( StringUtils.isEmpty(param.get("phone")) ){
            throw GlobalException.build("参数错误");
        }
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        String customerId;
        String orgId;
        if(!StringUtils.isEmpty(param.get("customerId"))){
            // 联汇管理总平台
            customerId = param.get("customerId");
            orgId = linkAccountService.getOrgIdByCustomerId(customerId);
        }else{
            // steplink 后台
            String adminId = userinfo.getAdminId();
            Boolean ifSales = userinfo.getSaler();
            orgId = userinfo.getOrgId();
            String type="1";
            if(ifSales){
                customerId = linkAccountService.getCustomerIdByAdminId(adminId);
                type = "2";
            }else{
                customerId = "";
            }
            customerId = xbMerchantMapper.getMerchantId(type,customerId,orgId);
        }
        Integer payMode = mallService.getPayModeByOrgId(orgId);
        String alias = "";
        if(payMode == 3){
            alias = maiBaoLaService.getAliasByLinker();
        }
        if(payMode == 2){
            alias = maiBaoLaService.getAlias(orgId);
        }
        param.put("clintType",alias);
        param.put("customerId",customerId);
        return linkAccountService.sendVerificationCode(param);
    }


    /**
     * 验证手机号
     * @param param
     * @param session
     * @return
     * @throws IOException
     * @throws URISyntaxException
     */
    @PostMapping("/tosigncontract")
    public Map toSignContract(@RequestBody Map<String, String> param, HttpServletRequest request) throws IOException, URISyntaxException {
        if(StringUtils.isEmpty(param.get("phone")) || StringUtils.isEmpty(param.get("verificationCode"))){
            throw GlobalException.build("参数错误");
        }
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        String customerId;
        String orgId;
        if(!StringUtils.isEmpty(param.get("customerId"))){
            // 联汇管理总平台
            customerId = param.get("customerId");
            orgId = linkAccountService.getOrgIdByCustomerId(customerId);
        }else{
            // steplink 后台
            String adminId = userinfo.getAdminId();
            Boolean ifSales = userinfo.getSaler();
            orgId = userinfo.getOrgId();
            String type="1";
            if(ifSales){
                customerId = linkAccountService.getCustomerIdByAdminId(adminId);
                type = "2";
            }else{
                customerId = "";
            }
            customerId = xbMerchantMapper.getMerchantId(type,customerId,orgId);
        }
        if(!StringUtils.isEmpty(param.get("jumpUrl"))){
            param.put("jumpUrl", URLEncoder.encode(param.get("jumpUrl"),"utf-8"));
        }
        Integer payMode = mallService.getPayModeByOrgId(orgId);
        String alias = "";
        if(payMode == 3){
            alias = maiBaoLaService.getAliasByLinker();
        }
        if(payMode == 2){
            alias = maiBaoLaService.getAlias(orgId);
        }
        param.put("clintType",alias);
        param.put("customerId",customerId);
        return linkAccountService.tosigncontract(param);
    }

    /**
     * 获取用户开户状态
     * @param jumpUrl
     * @param session
     * @return
     */
    @GetMapping("/accountstatus")
    public Map<String, Object> accountStatus(String jumpUrl, HttpServletRequest request) {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        String orgId = userinfo.getOrgId();
        if( StringUtils.isEmpty(jumpUrl) ){
            throw GlobalException.build("参数错误");
        }
        Boolean isSaler = userinfo.getSaler();
        String customerId = "";
        if(isSaler){
            String adminId = userinfo.getAdminId();
            customerId = linkAccountService.getCustomerIdByAdminId(adminId);
            customerId = purchaseProcessDao.getAccountUidByCustomerId(customerId);
            if( StringUtils.isEmpty(customerId) ){
                throw GlobalException.build("获取用户信息失败");
            }
        }else{
            customerId = purchaseProcessDao.getAccountUidByOrgId(orgId);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("customerId", customerId);
        map.put("jumpUrl", jumpUrl);
        map.put("orgId", orgId);
        map.put("realnameflag", false);
        return merchantWalletService.accountStatus(map);
    }
}
